<!DOCTYPE html>
<p>Test of Range.createContextualFragment() with in-scope namespace prefixes on attributes. If the test succeeds you will see the word "PASS" below.</p>
<p id="result"></p>
<script type="text/javascript">
function onIframeLoad() {
    if (window.testRunner)
        testRunner.dumpAsText();

    var result = document.getElementById("result");
    result.textContent = "FAIL";

    var iframe = document.getElementById("iframe"),
        doc = iframe.contentDocument,
        range = doc.createRange(),
        docFragment;

    var defs1 = doc.getElementById("defs1");
    range.setStart(defs1, 0);
    docFragment = range.createContextualFragment("<linearGradient id='gradient1'/>" +
                                                 "<linearGradient id='gradient2' XL:href='#gradient1' href='otherHref'/>");
    var gradient2 = docFragment.lastChild;
    if (gradient2.namespaceURI != "http://www.w3.org/2000/svg") {
        result.textContent += " - #gradient2 has the wrong namespace URI";
        return;
    }
    if (gradient2.getAttributeNS(defs1.lookupNamespaceURI("XL"), "href") != "#gradient1") {
        result.textContent += " - wrong XL:href attribute value on #gradient2";
        return;
    }
    defs1.appendChild(docFragment);
    if (gradient2.lookupNamespaceURI("XL") != defs1.lookupNamespaceURI("XL")) {
        result.textContent += " - #gradient2 returned the incorrect namespace URI for prefix 'XL'";
        return;
    }
    gradient2 = null;
    defs1 = null;

    var defs2 = doc.getElementById("defs2");
    range.setStart(defs2, 0);
    docFragment = range.createContextualFragment("<linearGradient id='gradient3'/>" +
                                                 "<linearGradient id='gradient4' xLink:href='#gradient3' href='otherHref'/>");
    defs2.appendChild(docFragment);
    var gradient4 = defs2.lastChild;
    if (gradient4.namespaceURI != "http://www.w3.org/2000/svg") {
        result.textContent += " - #gradient4 has the wrong namespace URI";
        return;
    }
    if (gradient4.getAttributeNS(defs2.lookupNamespaceURI("xLink"), "href") != "#gradient3") {
        result.textContent += " - wrong xLink:href attribute value on #gradient2";
        return;
    }
    if (gradient4.lookupNamespaceURI("xLink") != defs2.lookupNamespaceURI("xLink")) {
        result.textContent += " - #gradient2 returned the incorrect namespace URI for prefix 'xLink'";
        return;
    }

    // Check that Range.createContextualFragment() fails if an out-of-scope namespace prefix is used.
    var exception;
    try {
        range.createContextualFragment("<linearGradient id='gradient5' XL:href='#gradient3'/>");
    } catch (e) {
        exception = e;
    }
    if (exception == null) {
        result.textContent += " - Range.createContextualFragment() did not raise an exception when attempting to use an out-of-scope namespace prefix";
        return;
    }
    if (String.prototype.indexOf.call(exception.message, "invalid XML") < 0) {
        result.textContent += " - wrong exception thrown";
        return;
    }

    result.textContent = "PASS";
}
</script>
<iframe id="iframe" src="resources/svg-document-ns1.svg" onload="onIframeLoad()"></iframe>
